<template>
    <div class="detail-main">
        <el-dialog
            :title="'宿舍信息 --'+titleStatus "
            :visible.sync="show"
            width="60%"
            @close="handleClose">
            <el-form :model="form" ref="form" label-width="80px">
                <el-form-item label="宿舍名称">
                    <el-input v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="宿舍地址">
                    <el-input type="textarea" v-model="form.position"></el-input>
                </el-form-item>
                <el-form-item label="宿舍描述">
                    <el-input type="textarea" v-model="form.bak"></el-input>
                </el-form-item>
                <el-form-item label="宿舍类型">
                    <el-select v-model="form.type" placeholder="">
                        <el-option v-for="item in types"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                        </el-option>
                    </el-select>
                    
                </el-form-item>
                <el-form-item label="时间">
                    <el-date-picker type="date" value-format="yyyy-MM-dd" v-model="form.startTime" placeholder="开始时间">
                    </el-date-picker>
                    <el-date-picker type="date" value-format="yyyy-MM-dd" v-model="form.endTime" placeholder="结束时间">
                    </el-date-picker>
                </el-form-item>
                
            </el-form>
            
            <span slot="footer">
                <el-button @click="handleClose">取 消</el-button>
                <el-button type="primary" @click="doSave">确 定</el-button>
            </span>
        </el-dialog>
        
    </div>
</template>
<script>
import dormApi from '@/api/xy/dorm'
import {resetData} from '@/utils/index'
export default {
    name: 'DormDetail',
    data(){
        return{
            show: false,
            titleStatus:'新增',
            form:{},
            types:[
                {
                    label: '男',
                    value: 0
                },
                {
                    label: '女',
                    value: 1
                    
                },
                {
                    label: '未定',
                    value: 2
                    
                }
            ]
        }
    },
    methods: {
        openDialog(id){
            this.show = true;
            var api = null;
            if(id){
                this.titleStatus = '编辑'
                dormApi.detail({
                    id
                }).then(res=>{
                    this.form = res.data
                })
            }
        },
        handleClose(){
            resetData(this,'form')
            this.show = false;
        },
        doSave(){
            var api = null;
            //有id为编辑  否则为保存
            if(this.form.id){
                api = dormApi.updateDorm
                
            }else{
                api = dormApi.saveDorm
            }
            api({
                    xyDormInfo: {
                        ...this.form
                    }
                }).then(res=>{
                    if(res.code){
                        this.$message.success('保存成功!')
                        this.handleClose()
                        this.$parent.getDormList()
                    }else{
                        this.$message.error('保存失败!')
                    }
                })
        }
    }
}
</script>
<style lang='scss' scoped>
    .detail-main{

    }
</style>